import {Component} from '@angular/core';
import {Storage} from '@ionic/storage';

import {NavParams, ViewController} from 'ionic-angular';
// import {NativeService} from '../../../providers/NativeService';
// import {UserInfo} from "../../../model/UserInfo";
// import {Validators} from "../../../providers/Validators";
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import {UsersService} from '../../../providers/users.service';

@Component({
  selector: 'page-mine-edit-modal',
  templateUrl: 'mine-edit-modal.html',
  providers:[UsersService]

})
export class MineEditModalPage {
  userInfo: any;
  userForm: any;
  userName:any;
  personInfo:any;
  telephone:any;
  email:any;


  constructor(private params: NavParams,
              private viewCtrl: ViewController,
              private storage: Storage,
              private formBuilder: FormBuilder,
              private userSer:UsersService,

  ) {
    this.userInfo = this.params.get('userInfo');
    this.userForm = this.formBuilder.group({
      //
      userName: ['', Validators.compose([Validators.required, Validators.minLength(2)])],
      personInfo: ['', Validators.compose([Validators.required, Validators.minLength(5)])],
      telephone: ['', Validators.compose([Validators.required,  Validators.maxLength(11),Validators.minLength(11), Validators.pattern("^(13[0-9]|15[012356789]|17[03678]|18[0-9]|14[57])[0-9]{8}$")])],
      email: ['', Validators.compose([Validators.required, Validators.email])]
    });

    this.userName = this.userForm.controls['telephone'];
    this.personInfo = this.userForm.controls['personInfo'];
    this.telephone = this.userForm.controls['telephone'];
    this.email = this.userForm.controls['email'];

  }

  onSubmit(user) {
    let that = this;
    this.userSer.editUser(user,function (result) {
      if(result.stateCode==35){
        // alert(result.stateCode)
        this.storage.set('user',{"userName":result.userId,"userId":result.userId,"token":result.token});

      }else {
        // alert(result)
        console.log('提交失败，请稍后再试');
      }
    })
    Object.assign(this.userInfo, this.userForm.value);
    this.storage.set('UserInfo', this.userInfo);
    // this.nativeService.showToast('保存成功');
    this.viewCtrl.dismiss(this.userInfo);
  }

  dismiss() {
    this.viewCtrl.dismiss();
  }
}
